<template>
    <view class="loading-wrap" :style="[loadingStyle]">
        <image class="loading-image" src="/static/logo.png" mode="aspectFit"></image>
        <view class="loading-text">{{ text }}</view>
    </view>
</template>

<script>
/**
 * loading 加载中
 * @description 在页面显示一个全屏的loading动画
 * @param {String} text 加载中文字
 * @param {String|Number} duration 关闭时的动画时长
 * @event open loading被开启
 * @event close loading被关闭
 * @function open 开启loaidng
 * @function close 关闭loaidng
 */
export default {
    name: "Loading",
    props: {
		// 是否显示loading
        show: { type: Boolean, default: true },
		// loading文字
        text: { type: String, default: "加载中..." },
		// 关闭时的动画时长
        duration: { type: [Number, String], default: 500 }
    },
    data() {
        return {
			// 是否显示loading
            visible: false
        };
    },
    computed: {
        loadingStyle() {
            const style = {};
            if (this.visible === false) {
                style.opacity = 0;
                style.zIndex = -1;
                style.transition = `all ${this.duration / 1000}s ease-in-out`;
            }
            return style;
        }
    },
    methods: {
		// 开启loading
        open() {
			this.visible = true;
			this.$emit("open");
		},
		// 关闭loading
        close() {
			this.visible = false;
			this.$emit("close");
		},
    },
	mounted () {
		this.show && this.open();
	},
};
</script>

<style scoped lang="scss">
.loading-wrap {
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 1;
    z-index: 9999;
    display: flex;
    position: fixed;
    align-items: center;
    flex-direction: column;
    justify-content: center;
    background-color: #fff;
    &::after {
        content: "";
        position: absolute;
        background-color: #ffffff;
        border-radius: 50%;
        width: 200upx;
        height: 200upx;
        font-size: 10px;
        border-top: 6upx solid rgba(0, 0, 0, 0.05);
        border-right: 6upx solid rgba(0, 0, 0, 0.05);
        border-bottom: 6upx solid rgba(0, 0, 0, 0.05);
        border-left: 6upx solid $primary-color;
        animation: spin 1s infinite linear;
        z-index: -1;
    }
    .loading-image {
        width: 70rpx;
        height: 70rpx;
    }
    .loading-text {
        color: #606266;
        font-size: 24rpx;
        margin-top: 15rpx;
    }

    @keyframes spin {
        0% {
            -webkit-transform: rotate(0);
            transform: rotate(0);
        }

        100% {
            -webkit-transform: rotate(359deg);
            transform: rotate(359deg);
        }
    }
}
</style>
